﻿
CREATE PROCEDURE [dbo].[proc_Order_Create_G]
(
	@OId uniqueidentifier
	,@CusId	uniqueidentifier
	,@CusName	nvarchar(50)
	,@CusPerson	nvarchar(50)
	,@CusPersonTel	nvarchar(50)
	,@CusDescription	nvarchar(1000)
	,@CusEstimate	nvarchar(200)
	,@TypeId	int
	,@TypeName	nvarchar(50)
	,@WayId	int
	,@WayName	nvarchar(50)
	,@ProcessId	int
	,@ProcessName	nvarchar(50)
	,@Salesman	nvarchar(50)
	,@SalesmanAccount	nvarchar(50)
	,@TradeDate	datetime
	,@CreateDate	datetime
	,@shjbh	nvarchar(50)
	,@htbh nvarchar(50)
	,@YingShouPrice	decimal(18, 2)
	,@ShiShouPrice	decimal(18, 2)
	,@Discount	nvarchar(50)
	,@PaymentTypeId	int
	,@PaymentType	nvarchar(50)
	,@BankId	int
	,@Bank	nvarchar(50)
	,@PaymentDescription	nvarchar(200)
	,@OtherDescription	nvarchar(1000)
	,@DepId	int
	,@CompanyId	int
	,@StateId int
	,@NOE_Flag int
	,@CusName_BD nvarchar(200)
	,@CusRegTime datetime
	,@CusRegCapital decimal(18,2)
	,@CusEmployees int
	,@CusIntroduction nvarchar(max)
	,@CusPersonAge int
	,@CusPersonWeixin nvarchar(50)
)
AS
Begin
	Declare @nCount int,@tmpSumJe decimal(18, 2)

	If @CompanyId=0 Or @DepId =0
		Select @Salesman=EName,@DepId=DepartmentId,@CompanyId=CompanyId From Employee Where UserName=@SalesmanAccount
	Else
		Select @Salesman=EName From Employee Where UserName=@SalesmanAccount

	Set @nCount = 0		--初始化变量
	Select @nCount=Count(0),@tmpSumJe=Sum(UnitPrice*Quantity) From Order_Item Where Occupancy=@SalesmanAccount
	If @nCount = 0	--缺少成单项
		Begin
--			RollBack Tran
			Return 0
		End

	If @tmpSumJe <> @YingShouPrice	--成单项之和与应收不符
		Begin
--			RollBack Tran
			Return 0
		End

	Begin Tran
	If @NOE_Flag=0	--常规成单
			Insert Into [Order]
			(
				OId
				,CusId
				,CusName
				,CusPerson
				,CusPersonTel
				,CusDescription
				,CusEstimate
				,TypeId
				,TypeName
				,WayId
				,WayName
				,ProcessId
				,ProcessName
				,Salesman
				,SalesmanAccount
				,TradeDate
				,CreateDate
				,YingShouPrice
				,ShiShouPrice
				,Discount
				,PaymentTypeId
				,PaymentType
				,BankId
				,Bank
				,PaymentDescription
				,OtherDescription
				,DepId
				,CompanyId
				,StateId
				,IsGreen
				,shjbh
				,htbh
				,NOE_Flag
				,ExCompanyId
				,F_YingShou	--首次录入时的成单总额，财报统计专用(2012-09-22)
				,CusName_BD
	,H_YingShou
				)
			Values
				(
				@OId
				,@CusId
				,@CusName
				,@CusPerson
				,@CusPersonTel
				,@CusDescription
				,@CusEstimate
				,@TypeId
				,@TypeName
				,@WayId
				,@WayName
				,@ProcessId
				,@ProcessName
				,@Salesman
				,@SalesmanAccount
				,@TradeDate
				,@CreateDate
				,@YingShouPrice
				,@ShiShouPrice
				,@Discount
				,@PaymentTypeId
				,@PaymentType
				,@BankId
				,@Bank
				,@PaymentDescription
				,@OtherDescription
				,@DepId
				,@CompanyId
				,@StateId
				,0
				,@shjbh
				,@htbh
				,@NOE_Flag
				,@CompanyId
				,@YingShouPrice	--首次录入时的成单总额，财报统计专用(2012-09-22)
				,@CusName_BD
		,@YingShouPrice-@ShiShouPrice	--记录此时的应收款，作为历史查询用 By ZHT 2014-12-25
				)
	Else	--非常规(含非企)成单
		Insert Into [Order]
		(
			OId
			,CusId
			,CusName
			,CusPerson
			,CusPersonTel
			,CusDescription
			,CusEstimate
			,TypeId
			,TypeName
			,WayId
			,WayName
			,ProcessId
			,ProcessName
			,Salesman
			,SalesmanAccount
			,TradeDate
			,CreateDate
			,YingShouPrice
			,ShiShouPrice
			,Discount
			,PaymentTypeId
			,PaymentType
			,BankId
			,Bank
			,PaymentDescription
			,OtherDescription
			,DepId
			,CompanyId
			,StateId
			,IsGreen
			,TrackKfAccount
			,TrackKfName
			,TrackKfDepId
			,shjbh
			,htbh
			,NOE_Flag
			,ExCompanyId
			,F_YingShou	--首次录入时的成单总额，财报统计专用(2012-09-22)
			,CusName_BD
	,H_YingShou
		)
		Values
		(
			@OId
			,@CusId
			,@CusName
			,@CusPerson
			,@CusPersonTel
			,@CusDescription
			,@CusEstimate
			,@TypeId
			,@TypeName
			,@WayId
			,@WayName
			,@ProcessId
			,@ProcessName
			,@Salesman
			,@SalesmanAccount
			,@TradeDate
			,@CreateDate
			,@YingShouPrice
			,@ShiShouPrice
			,@Discount
			,@PaymentTypeId
			,@PaymentType
			,@BankId
			,@Bank
			,@PaymentDescription
			,@OtherDescription
			,@DepId
			,@CompanyId
			,@StateId
			,0
			,@SalesmanAccount
			,@Salesman
			,@DepId
			,@shjbh
			,@htbh
			,@NOE_Flag
			,@CompanyId
			,@YingShouPrice	--首次录入时的成单总额，财报统计专用(2012-09-22)
			,@CusName_BD
	,@YingShouPrice-@ShiShouPrice	--记录此时的应收款，作为历史查询用 By ZHT 2014-12-25
		)

	Insert Into Order_PaymentRecord
	(
		OId
		,CusId
		,CusName
		,SalesmanAccount
		,Salesman
		,CreateDate
		,Prices
		,PaymentTypeId
		,PaymentType
		,BankId
		,Bank
		,SWId
		,SWName
		,FinanceFlag
		,CompanyId
	)
	Values
	(
		@OId
		,@CusId
		,@CusName
		,@SalesmanAccount
		,@Salesman
		,@CreateDate
		,@ShiShouPrice
		,@PaymentTypeId
		,@PaymentType
		,@BankId
		,@Bank
		,1
		,'首款'
		,0
		,@CompanyId
	)

	Update Order_Item Set OId=@OId,Occupancy=NULL,CreateDate=Getdate(),TradeDate=@TradeDate Where Occupancy=@SalesmanAccount
	Update Customer Set 
		StateId=2,
		ProtectId=3,
		ProtectName=NULL,
		ProtectDate=NULL,
		RegTime=@CusRegTime,
		RegCapital=@CusRegCapital,
		Employees=@CusEmployees,
		Introduction=@CusIntroduction
	Where CustomerId=@CusId

	Declare @cpId int;
	Select Top 1 @cpId=Id From ContactPerson 
		Where CustomerId=@CusId And 
		((Mobile<>'' And (Mobile Is Not Null)) Or 
		(OfficeTel<>'' And (OfficeTel Is Not Null)) Or 
		(HomeTel<>'' And (HomeTel Is Not Null)))
		Order By IsMain Desc,PersonTypeId,CreateDate Desc
	--更新联系人年龄、微信
	Update ContactPerson Set
		Age=@CusPersonAge,
		Weixin=@CusPersonWeixin
	where Id=@cpId

	If @@error <> 0  --发生错误
		Begin
			RollBack Tran
			Return 3
		End
	Else
		Begin
			Commit Tran
			Return 1	--执行成功
		End
End